﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using EstateTrackerObjects.Enumerations;
using System.Data;
using System.Data.SqlClient;

namespace EstateTrackerObjects.Collections
{
    public class Players : IList<Player>
    {

        private readonly IList<Player> _player;
        private string connectionString;

        public Players()
        {
            _player = new List<Player>();
            connectionString = EstateTrackerObjects.Properties.Settings.Default.BeronisDBConnectionStringLive;
        }

        public int Load()
        {
            string sql = "SELECT ID, playerName, emailAddress FROM Players";
            int count = 0;
            using (SqlConnection connection = new SqlConnection(connectionString))            
                using (SqlCommand cmd = new SqlCommand(sql, connection))
                {
                    connection.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    Player p;
                    while (dr.Read())
                    {
                        var ID = (int)dr[0];
                        var name = dr[1] as string;
                        var email = dr[2] as string;
                        p = new Player(ID, name,email);
                        this.Add(p);
                        count++;
                    }
                }

            return count;
        }



        #region CollectionBase Functions
        
        public void Add(Player item)
        {
            _player.Add(item);
        }

        public void Clear()
        {
            _player.Clear();
        }

        public bool Contains(Player item)
        {
            return _player.Contains(item);
        }

        public void CopyTo(Player[] array, int arrayIndex)
        {
            _player.CopyTo(array,arrayIndex);
        }

        public int Count
        {
            get { return _player.Count(); }
        }

        public bool IsReadOnly
        {
            get { return _player.IsReadOnly; }
        }

        public bool Remove(Player item)
        {
            return _player.Remove(item);
        }

        public IEnumerator<Player> GetEnumerator()
        {
            return _player.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _player.GetEnumerator();
        }

        public int IndexOf(Player item)
        {
            return _player.IndexOf(item);
        }

        public void Insert(int index, Player item)
        {
            _player.Insert(index,item);
        }

        public void RemoveAt(int index)
        {
            _player.RemoveAt(index);
        }

        public Player this[int index]
        {
            get
            {
                return _player[index];
            }
            set
            {
                _player[index] = value;
            }
        }
        #endregion
    }
}
